﻿using CompanyManagement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CompanyManagement
{
    public partial class Login : System.Web.UI.Page
    {
        AccountDAO accountDao;
        public Login()
        {
            accountDao = new AccountDAO();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                Response.Redirect("~/management/Companies.aspx");
            }
            if (!IsPostBack)
            {
                if (Request.Browser.Cookies)
                {
                    if (Request.Cookies["UserInfor"] != null)
                    {
                        HttpCookie cookUserInfo = Request.Cookies["UserInfor"];
                        username.Value = cookUserInfo["UserName"];
                        txtPassword.Attributes["value"] = cookUserInfo["Password"];
                    }
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = username.Value.Trim();
            string pass = txtPassword.Text;
            int result = accountDao.Login(userName, pass);


            if (loginkeeping.Checked)
            {
                if (Request.Browser.Cookies)
                {
                    if (result > 0)
                    {
                        if (Request.Cookies["UserInfor"] == null)
                        {
                            // Create a cookie.
                            HttpCookie cookUserInfo = new HttpCookie("UserInfor");
                            // Fill in the keys from the form data.
                            cookUserInfo["UserName"] = username.Value.Trim();
                            cookUserInfo["Password"] = txtPassword.Text;
                            // Set the expiration.
                            cookUserInfo.Expires = DateTime.Now.AddDays(1);
                            // Add the cookie.
                            Response.Cookies.Add(cookUserInfo);
                        }
                        else
                        {
                            HttpCookie cookUserInfo = Request.Cookies["UserInfor"];
                            // Fill in the keys from the form data.
                            cookUserInfo["UserName"] = username.Value.Trim();
                            cookUserInfo["Password"] = txtPassword.Text;
                            // Set the expiration.
                            cookUserInfo.Expires = DateTime.Now.AddDays(1);
                            // Add the cookie.
                            Response.Cookies.Add(cookUserInfo);
                        }
                        // (2) If the cookie does not exist...
                        if (Request.Cookies["LastVisits"] == null)
                        {
                            // (3) Create the cookie.
                            HttpCookie cookLastVisit = new HttpCookie("LastVisits", DateTime.Now.ToString());
                            // (4) Set the expiration to tommorrow.
                            cookLastVisit.Expires = DateTime.Now.AddDays(1);
                            // (5) Add to cookies collection.
                            Response.Cookies.Add(cookLastVisit);
                            // Display message.
                            //litMessage.Text = "This is your first visit.";
                        }
                        else
                        {
                            // Get the cookie.
                            HttpCookie cookLastVisit = Request.Cookies["LastVisits"];
                            // Display a message showing time of last visit.
                            //litMessage.Text = "You last visited this page: " + cookLastVisit.Value;
                            // Update the cookie on the client.
                            Response.Cookies["LastVisits"].Value = DateTime.Now.ToString();
                            Response.Cookies["LastVisits"].Expires = DateTime.Now.AddDays(1);
                            Response.Cookies.Add(cookLastVisit);
                        }
                    }
                    else
                    {
                        if (Request.Browser.Cookies)
                        {
                            // Create a cookie.
                            HttpCookie cookUserInfo = new HttpCookie("UserInfor");
                            cookUserInfo.Expires = DateTime.Now;
                            // Add the cookie.
                            Response.Cookies.Add(cookUserInfo);
                        }
                    }
                }
            }
            
            if (result > 0)
            {
                Session["user"] = userName;
                Session["usertype"] = result;
                if(result == 2)
                    Response.Redirect("~/management/Companies.aspx");
                Response.Redirect("~/admin/Accounts.aspx");
            }
            else
            {
                CommonClass.MessageBox.Show("Sai tên đăng nhập hoặc mật khẩu. Vui lòng nhập lại.");
                username.Focus();
            }
        }
    }
}